//-----------------------------------------------------------------------------
//	File: key.c
//	Author: hsia
//		bsp of keys
//-----------------------------------------------------------------------------

#include "key.h"

#define KEY_RCC_ALL		(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC)
#define	KEY_1_PORT		GPIOA
#define	KEY_1_PIN		GPIO_Pin_7
#define	KEY_2_PORT		GPIOC
#define	KEY_2_PIN		GPIO_Pin_5

// KEY_DOWN_GPIO_HIGH - press key gpio=1
// KEY_DOWN_GPIO_LOW - press key gpio=0
#define	KEY_DOWN_GPIO_LOW

//-----------------------------------------------------------------------------
//	configure_key_gpio - keys gpio configure
//-----------------------------------------------------------------------------
void configure_key_gpio(void)
{
	GPIO_InitTypeDef	GPIO_InitStructure;

	// Enable the peripheral's clock
	RCC_APB2PeriphClockCmd(KEY_RCC_ALL, ENABLE);
	// gpio mode
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	// keys pin seting
	GPIO_InitStructure.GPIO_Pin = KEY_1_PIN;
	GPIO_Init(KEY_1_PORT, &GPIO_InitStructure);
	GPIO_InitStructure.GPIO_Pin = KEY_2_PIN;
	GPIO_Init(KEY_2_PORT, &GPIO_InitStructure);
}

//-----------------------------------------------------------------------------
//	key_scan - scan the keys
//-----------------------------------------------------------------------------
uint16_t key_scan(void)
{
	uint16_t key_state = 0x0000;

#ifdef KEY_DOWN_GPIO_HIGH
	key_state |= ((KEY_1_PORT->IDR & KEY_1_PIN) ? 1u : 0u) << 0;
	key_state |= ((KEY_2_PORT->IDR & KEY_2_PIN) ? 1u : 0u) << 1;
//	key_state |= ((KEY_3_PORT->IDR & KEY_3_PIN) ? 1u : 0u) << 2;
//	key_state |= ((KEY_4_PORT->IDR & KEY_4_PIN) ? 1u : 0u) << 3;
//	key_state |= ((KEY_5_PORT->IDR & KEY_5_PIN) ? 1u : 0u) << 4;
//	key_state |= ((KEY_6_PORT->IDR & KEY_6_PIN) ? 1u : 0u) << 5;
//	key_state |= ((KEY_7_PORT->IDR & KEY_7_PIN) ? 1u : 0u) << 6;
//	key_state |= ((KEY_8_PORT->IDR & KEY_8_PIN) ? 1u : 0u) << 7;
//	key_state |= ((KEY_9_PORT->IDR & KEY_9_PIN) ? 1u : 0u) << 8;
//	key_state |= ((KEY_10_PORT->IDR & KEY_10_PIN) ? 1u : 0u) << 9;
//	key_state |= ((KEY_11_PORT->IDR & KEY_11_PIN) ? 1u : 0u) << 10;
//	key_state |= ((KEY_12_PORT->IDR & KEY_12_PIN) ? 1u : 0u) << 11;
//	key_state |= ((KEY_13_PORT->IDR & KEY_13_PIN) ? 1u : 0u) << 12;
//	key_state |= ((KEY_14_PORT->IDR & KEY_14_PIN) ? 1u : 0u) << 13;
//	key_state |= ((KEY_15_PORT->IDR & KEY_15_PIN) ? 1u : 0u) << 14;
//	key_state |= ((KEY_16_PORT->IDR & KEY_16_PIN) ? 1u : 0u) << 15;
#elif defined KEY_DOWN_GPIO_LOW
	key_state |= ((KEY_1_PORT->IDR & KEY_1_PIN) ? 0u : 1u) << 0;
	key_state |= ((KEY_2_PORT->IDR & KEY_2_PIN) ? 0u : 1u) << 1;
//	key_state |= ((KEY_3_PORT->IDR & KEY_3_PIN) ? 0u : 1u) << 2;
//	key_state |= ((KEY_4_PORT->IDR & KEY_4_PIN) ? 0u : 1u) << 3;
//	key_state |= ((KEY_5_PORT->IDR & KEY_5_PIN) ? 0u : 1u) << 4;
//	key_state |= ((KEY_6_PORT->IDR & KEY_6_PIN) ? 0u : 1u) << 5;
//	key_state |= ((KEY_7_PORT->IDR & KEY_7_PIN) ? 0u : 1u) << 6;
//	key_state |= ((KEY_8_PORT->IDR & KEY_8_PIN) ? 0u : 1u) << 7;
//	key_state |= ((KEY_9_PORT->IDR & KEY_9_PIN) ? 0u : 1u) << 8;
//	key_state |= ((KEY_10_PORT->IDR & KEY_10_PIN) ? 0u : 1u) << 9;
//	key_state |= ((KEY_11_PORT->IDR & KEY_11_PIN) ? 0u : 1u) << 10;
//	key_state |= ((KEY_12_PORT->IDR & KEY_12_PIN) ? 0u : 1u) << 11;
//	key_state |= ((KEY_13_PORT->IDR & KEY_13_PIN) ? 0u : 1u) << 12;
//	key_state |= ((KEY_14_PORT->IDR & KEY_14_PIN) ? 0u : 1u) << 13;
//	key_state |= ((KEY_15_PORT->IDR & KEY_15_PIN) ? 0u : 1u) << 14;
//	key_state |= ((KEY_16_PORT->IDR & KEY_16_PIN) ? 0u : 1u) << 15;
#endif

	return key_state;
}

/*
//-----------------------------------------------------------------------------
//	example
//-----------------------------------------------------------------------------
{
	uint16_t key_trig;
	uint16_t key_hold;
	uint16_t key_down;
	uint16_t key_up;

	uint16_t key_state;

	key_state = key_scan();
	key_trig = key_state ^ key_hold;
	key_hold = key_state;
	key_down = key_trig & key_hold;
	key_up = key_trig & (key_trig ^ key_hold);

	if (key_up & KEY_1) {
		;
	}
}
*/

